import { IDevActionHelper } from '@/core';
/**
 * Debug工具类
 *
 * @export
 * @class DevActionHelper
 */
export class DevActionHelper implements IDevActionHelper {
	/**
	 * 唯一实例
	 *
	 * @private
	 * @static
	 * @type {DevActionHelper}
	 * @memberof DevActionHelper
	 */
	private static service: IDevActionHelper;

	/**
	 * 显示工具栏
	 *
	 * @type { boolean }
	 * @memberof DevActionHelper
	 */
	private showTool: boolean = false;

	/**
	 * 回调数组
	 *
	 * @private
	 * @type {Array<Function>}
	 * @memberof DevActionHelper
	 */
	private callBacks: Array<Function> = [];

	/**
	 * 获取实例
	 *
	 * @static
	 * @return {*}  {DevActionHelper}
	 * @memberof DevActionHelper
	 */
	static getInstance(): IDevActionHelper {
		if (!this.service) {
			this.service = new DevActionHelper();
		}
		return this.service;
	}

	/**
	 * 获取展示工具栏状态
	 *
	 * @return {*}  {boolean}
	 * @memberof IDevActionHelper
	 */
	public getShowTool(): boolean {
		return this.showTool;
	}

	/**
	 * 展示配置工具栏状态变更
	 *
	 * @memberof DevActionHelper
	 */
	public changeShowTool(): void {
		this.showTool = !this.showTool;
		if (this.callBacks.length > 0) {
			this.callBacks.forEach((callBack: Function) => {
				callBack(this.showTool);
			});
		}
	}

	/**
	 * 添加变化回调
	 *
	 * @memberof DevActionHelper
	 */
	public addChangeCallBack(callback: (result: boolean) => void) {
		this.callBacks.push(callback);
	}

	/**
	 * 删除变化回调
	 *
	 * @memberof DevActionHelper
	 */
	public removeChangeCallBack(callback: (result: boolean) => void) {
		if (this.callBacks.length > 0) {
			const index = this.callBacks.findIndex((item: Function) => {
				return item === callback;
			});
			this.callBacks.splice(index, 1);
		}
	}
}
